#pragma once

#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <iterator>
using namespace std;

#include "building.h"

class buildingDatabase : public vector<building>
{
public:

	buildingDatabase()
	{
	}

	~buildingDatabase()
	{
	}

	void writeFile(string filename)
	{
		ofstream out(filename);
		for (int i = 0; i < this->size(); i++){
			out << "building=" << (*this)[i].getInfo() << endl;
			out << (*this)[i].list();
			out << "endbuilding=" << endl;
		}
	}

	void readFile(string filename)
	{
		this->clear();
		ifstream in(filename);
		string line, s;
		building b;
		room r;
		while (getline(in, line))
		{
			istringstream iss(line);
			istringstream iss2(line);
			getline(iss, s, '=');
			if (s == "building")
			{
				getline(iss, s);
				b.setInfo(s);
			}
			else if (s == "type")
			{
				iss2 >> r;
				b.add(r);
			}
			else if (s == "endbuilding")
			{
				this->push_back(b);
				b.clear();
			}
		}
	}

	double area()
	{
		double area = 0;
		for (int i = 0; i < this->size(); i++)
		{
			area += (*this)[i].area();
		}
		return area;
	}

	iterator search(string info)
	{
		iterator it;
		for (it = this->begin(); it < this->end(); it++){
			if (it->getInfo() == info){
				return it;
			}
		}
		cout << "No such building!" << endl;
		return it;
	}

	void newBuilding()
	{
		string info;
		building b;
		int numrooms;

		cout << "Ange byggnadens info: ";
		getline(cin, info);
		b.setInfo(info);
		cout << "Ange antal rum: ";
		cin >> numrooms;

		for (int i = 0; i < numrooms; i++){
			room r;
			r.read();
			b.add(r);
		}

		this->push_back(b);
	}

	void deleteBuilding(string info)
	{
		iterator it = search(info);
		if (it != this->end())
		{
			this->erase(it);
		}
	}
};

